using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
namespace PongXNA
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        

        
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        /// <summary>
        ///  TODO Decleration of variable used in this program
        /// </summary>
        ///

        private Scene scene = new Scene();
        private Racket firstRacket_obj; 
        private Racket secondRacket_obj;
        private Ball ball_obj;
        enum GameState
        {
            Pause,
            Run,
        }
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            this.IsMouseVisible = true; //  Allow the mouse to be visible
            firstRacket_obj = new Racket(Content.Load<Texture2D>("FirstRacket"));
            firstRacket_obj.RacketInit(new Rectangle((int)0, (int)GraphicsDevice.Viewport.Height /2 - firstRacket_obj.SpriteRacket.Height /2, firstRacket_obj.SpriteRacket.Width, firstRacket_obj.SpriteRacket.Height),1);

            secondRacket_obj = new Racket(Content.Load<Texture2D>("SecondRacket"));
            secondRacket_obj.RacketInit(new Rectangle(GraphicsDevice.Viewport.Width - secondRacket_obj.SpriteRacket.Width , GraphicsDevice.Viewport.Height / 2 - firstRacket_obj.SpriteRacket.Height / 2, secondRacket_obj.SpriteRacket.Width, secondRacket_obj.SpriteRacket.Height), 1);

            ball_obj = new Ball(Content.Load<Texture2D>("Ball"));
            ball_obj.BallInit(
                new Rectangle(GraphicsDevice.Viewport.Width/2 ,GraphicsDevice.Viewport.Height/2 ,ball_obj.SpriteBall.Width,ball_obj.SpriteBall.Height),
                new Vector2(-1, -1),
                1,
                4);

            Components.Add(new GamerServicesComponent(this));
                        
            scene.SpriteFont = Content.Load<SpriteFont>("Times New Roman");
            
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);  
            // TODO: use this.Content to load your game content here
            // Go To Iniatialize() no reason to initialize() everywhere
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            secondRacket_obj.Etat = "Keyboard";
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            
            // TODO: Add your update logic here
            MouseState mouseState = Mouse.GetState(); // We just want to have the current position of mouse

            KeyboardState UpOrDown = Keyboard.GetState();
            if ((mouseState.Y > firstRacket_obj.HitBox.Height / 4) && (mouseState.Y < GraphicsDevice.Viewport.Height - firstRacket_obj.HitBox.Height / 4)) // 1/4
            {
                firstRacket_obj.BougerRacket(firstRacket_obj.HitBox.X, mouseState.Y - firstRacket_obj.HitBox.Height / 2);// to put the middle of the racket  in front a the mouse               
            }
            else
            {

            }
            if(secondRacket_obj.Etat == "Keyboard")
            {
                if(UpOrDown.IsKeyDown(Keys.Down) && (secondRacket_obj.HitBox.Y <  GraphicsDevice.Viewport.Height - 3*secondRacket_obj.HitBox.Height/4)) //  1 /4 + 3/4
                {
                    secondRacket_obj.BougerRacket(secondRacket_obj.HitBox.X, secondRacket_obj.HitBox.Y + 3);
                }
                else if(UpOrDown.IsKeyDown(Keys.Up) && (secondRacket_obj.HitBox.Y > 0 - secondRacket_obj.HitBox.Height/4)) // 1/4
                {
                    secondRacket_obj.BougerRacket(secondRacket_obj.HitBox.X, secondRacket_obj.HitBox.Y - 3);
                }
                else
                {

                }
            }
            else if (secondRacket_obj.Etat == "IA")
            {

            }
            else if(secondRacket_obj.Etat == "Multiplayer")
            {
                //// TODO Create a socket between client & server
                //// 

            }

            if (ball_obj.HitBox.X < 0 || ball_obj.HitBox.X > GraphicsDevice.Viewport.Width)
            {
                ball_obj.VecteurVitesse = new Vector2(ball_obj.VecteurVitesse.X * -1 , ball_obj.VecteurVitesse.Y);
            }
            else if (ball_obj.HitBox.Y < 0 || ball_obj.HitBox.Y > GraphicsDevice.Viewport.Height)
            {
                ball_obj.VecteurVitesse = new Vector2(ball_obj.VecteurVitesse.X, ball_obj.VecteurVitesse.Y*-1);
            }
                ball_obj.BougerBall();




           

           // if (ball_obj.VectorPosition.X < 0)
           // {
           //     //point a droite
           //     ball_obj.VectorPosition = new Vector2(GraphicsDevice.Viewport.Width / 2 - ball_obj.HitBox.Width / 2, GraphicsDevice.Viewport.Height / 2 - ball_obj.HitBox.Height / 2);
           //     scene.ScorePlayerOne++;
           //     ball_obj.VecteurVitesse = new Vector2(-1, 0);                
           //     ball_obj.Vitesse= 2;
           // }
           // else if (ball_obj.VectorPosition.X > GraphicsDevice.Viewport.Width)
           // {
           //     //point a gauche
           //     ball_obj.VectorPosition = new Vector2(GraphicsDevice.Viewport.Width / 2 - ball_obj.HitBox.Width / 2, GraphicsDevice.Viewport.Height / 2 - ball_obj.HitBox.Height / 2);
           //     scene.ScorePlayerTwo++;
           //     ball_obj.VecteurVitesse = new Vector2(1, 0);
           //     ball_obj.Vitesse = 2;
           // }
           // else if (ball_obj.VectorPosition.Y < 0 || ball_obj.VectorPosition.Y + ball_obj.SpriteBall.Height > GraphicsDevice.Viewport.Height)
           // {
           //     ball_obj.VecteurVitesse = new Vector2(ball_obj.VecteurVitesse.X, ball_obj.VecteurVitesse.Y * -1);
                
           // }
           // else if (firstRacket_obj.HitBox.Contains(ball_obj.HitBox))
           // {
           //     ball_obj.VecteurVitesse = new Vector2(ball_obj.VecteurVitesse.X * -1, (float)(ball_obj.VecteurVitesse.Y + ball_obj.VectorPosition.Y + ball_obj.HitBox.Height / 2 - firstRacket_obj.VectorPosition.Y - firstRacket_obj.HitBox.Height / 2) / 14); // Pk /14
           //     ball_obj.Vitesse++;
           // }
           // else if (secondRacket_obj.HitBox.Contains(ball_obj.HitBox))
           // {
           //     ball_obj.VecteurVitesse = new Vector2(ball_obj.VecteurVitesse.X * -1, (float)(ball_obj.VecteurVitesse.Y + ball_obj.VectorPosition.Y + ball_obj.HitBox.Height / 2 - secondRacket_obj.VectorPosition.Y - secondRacket_obj.HitBox.Height / 2) / 14); // Pk /14
           //     ball_obj.Vitesse++;               
           // }
           // else
           // { }
           // ball_obj.VecteurVitesse.Normalize();
           // ball_obj.VectorPosition = new Vector2(ball_obj.VectorPosition.X + ball_obj.VecteurVitesse.X * ball_obj.Vitesse, ball_obj.VectorPosition.Y + ball_obj.VecteurVitesse.Y * ball_obj.Vitesse);

            //firstRacket_obj.HitBox = new Rectangle((int)80, (int)80, 40, 40);
            //secondRacket_obj.HitBox = new Rectangle((int)secondRacket_obj.VectorPosition.X, (int)secondRacket_obj.VectorPosition.Y, secondRacket_obj.HitBox.X, secondRacket_obj.HitBox.Y);
            //ball_obj.HitBox = new Rectangle((int)ball_obj.VectorPosition.X, (int)ball_obj.VectorPosition.Y, ball_obj.SpriteBall.Width, ball_obj.SpriteBall.Height);
                
            base.Update(gameTime);

        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            graphics.GraphicsDevice.Clear(Color.White);
            spriteBatch.Begin();
            spriteBatch.Draw(firstRacket_obj.SpriteRacket,firstRacket_obj.HitBox, Color.White);
            spriteBatch.Draw(secondRacket_obj.SpriteRacket, secondRacket_obj.HitBox, Color.White);
            spriteBatch.Draw(ball_obj.SpriteBall, ball_obj.HitBox, Color.White);
            
            spriteBatch.DrawString(scene.SpriteFont, scene.Score(), new Vector2(GraphicsDevice.Viewport.Width / 2 - scene.SpriteFont.MeasureString(scene.Score()).X /2, 10), Color.Black);
            spriteBatch.End();
            base.Draw(gameTime);
        }

        
    }
}
